﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Backend.JYD.Model;
using Backend.JYD.Common;
using System.Web;

namespace Backend.JYD.RunTime
{
    public class SessionMgr
    {
        private const string Jyd_UserInfo_Emp = "Jyd_UserInfo_Emp";
        private const string Jyd_UserInfo_Name = "Jyd_UserInfo_Name";
        private const string Jyd_UserInfo_Date = "Jyd_UserInfo_Date";
        private const string Jyd_UserCookie = "Jyd_UserCookie";

        public static EmpInfo GetEmpInfoFromSession(HttpContextBase context)
        {
            //object userid = System.Web.HttpContext.Current.Session[Jyd_UserInfo_Emp];
            //object name = System.Web.HttpContext.Current.Session[Jyd_UserInfo_Name];
            //object login = System.Web.HttpContext.Current.Session[Jyd_UserInfo_Date];
            if (context.Session == null) return null;
            object userid = context.Session[Jyd_UserInfo_Emp];
            object name = context.Session[Jyd_UserInfo_Name];
            object login = context.Session[Jyd_UserInfo_Date];

            //HttpCookie userCookie = context.Request.Cookies[Jyd_UserCookie];
            //if (userCookie == null) return null;
            //object userid = userCookie[Jyd_UserInfo_Emp];
            //object name = userCookie[Jyd_UserInfo_Name];
            //object login = userCookie[Jyd_UserInfo_Date];
            if (userid == null || name == null)
            {
                return null;
            }

            EmpInfo info = new EmpInfo();
            info.EmpId = NumberHelper.StrToInt64(EncryptHelper.Decrypt(userid.ToString()));
            info.EmpName = EncryptHelper.Decrypt(name.ToString());

            //DateTime loginDate = DateTimeHelper.Str2DateTime(EncryptHelper.Decrypt(login.ToString()), DateTime.Now);
            return info.EmpId <= 0 ? null : info;
        }

        public static void ResetSession(HttpContextBase context, EmpInfo info)
        {
            if (info == null) return;

            //HttpCookie userCookie = context.Request.Cookies[Jyd_UserCookie];
            //if (userCookie == null || userCookie[Jyd_UserInfo_Emp] == null
            //    || string.IsNullOrWhiteSpace(userCookie[Jyd_UserInfo_Emp].ToString()) || NumberHelper.ObjectToInt64(userCookie[Jyd_UserInfo_Emp]) <= 0)
            //{
            //    userCookie = new HttpCookie(Jyd_UserCookie);
            //    userCookie[Jyd_UserInfo_Emp] = EncryptHelper.Encrypt(info.EmpId.ToString());
            //    userCookie[Jyd_UserInfo_Name] = EncryptHelper.Encrypt(info.EmpName.ToString());
            //    userCookie[Jyd_UserInfo_Date] = EncryptHelper.Encrypt(DateTimeHelper.GetDateFormart(DateTime.Now, DateTimeHelper.MILLI_SECOND_DATE_TIME));
            //}
            //userCookie.Expires = DateTime.Now.AddMinutes(30);

            if (context.Session == null) return;
            //context.Session[Jyd_UserInfo_Emp] = userCookie[Jyd_UserInfo_Emp];
            //context.Session[Jyd_UserInfo_Name] = userCookie[Jyd_UserInfo_Name];
            //context.Session[Jyd_UserInfo_Date] = userCookie[Jyd_UserInfo_Date];
            context.Session[Jyd_UserInfo_Emp] = EncryptHelper.Encrypt(info.EmpId.ToString());
            context.Session[Jyd_UserInfo_Name] = EncryptHelper.Encrypt(info.EmpName.ToString());
            context.Session[Jyd_UserInfo_Date] = EncryptHelper.Encrypt(DateTimeHelper.GetDateFormart(DateTime.Now, DateTimeHelper.MILLI_SECOND_DATE_TIME));
        }

        public static void ClearSeession(HttpContextBase context)
        {
            //HttpCookie userCookie = context.Request.Cookies[Jyd_UserCookie];
            //if (userCookie == null) return;
            //userCookie.Expires = DateTime.Now.AddSeconds(-1);

            if (context.Session == null) return;
            context.Session.Remove(Jyd_UserInfo_Emp);
            context.Session.Remove(Jyd_UserInfo_Name);
            context.Session.Remove(Jyd_UserInfo_Date);
        }
    }
}
